var gulp        = require('gulp');
var browserSync = require('browser-sync').create();
var less = require('gulp-less');
var path = require('path');
var spritesmith = require('gulp.spritesmith');
var usemin = require('gulp-usemin');
var minifyCss = require('gulp-minify-css');
var minifyHtml = require('gulp-minify-html');
var rev = require('gulp-rev');
var zip = require('gulp-zip');
var jshint = require('gulp-jshint');
var taskListing = require('gulp-task-listing');
var templateCache = require('gulp-angular-templatecache');
var uglify = require('gulp-uglify');


var paths = {
    css: ['./styles/*.css'],
    html: ['./views/*.html'],
    img: ['./images/*.*'],
    buildPath: './build'

};



// 生成合并图片
gulp.task('sprite', function () {
  var spriteData = gulp.src('./styles/images/icon/*.png').pipe(spritesmith({
    imgName: 'sprite-icon.png',
    cssName: 'sprite-icon.css'
  }));
  return spriteData.pipe(gulp.dest('./styles/'));
});

gulp.task('spriteWX', function () {
  var spriteData = gulp.src('./styles/images/wx-icon/*.png').pipe(spritesmith({
    imgName: 'sprite-wxicon.png',
    cssName: 'sprite-wxicon.css'
  }));
  return spriteData.pipe(gulp.dest('./styles/'));
});

gulp.task('less', function () {
  return gulp.src('./styles/**/*.less')
    .pipe(less({
      paths: [ path.join(__dirname, 'less', 'includes') ]
    }))
    .pipe(gulp.dest('./styles'))
    .pipe(browserSync.stream());
});


/**
 * 压缩修改html文件
 */
gulp.task('usemin', function(){
    // 压缩index文件
    gulp.src('./index.html')
        .pipe(usemin({
            indexCSS: [minifyCss(), 'concat', rev()],	// 压缩css、合并、添加版本号
            jsLibs: [rev()],
            jsMain: [uglify(), rev()],
            jsTpl: [rev()],
            html: [minifyHtml({empty:true, quotes: true})]
        }))
        .pipe(gulp.dest(paths.buildPath));
});

/**
 * 压缩views
 * @param  {[type]} 'minifyHtml' [description]
 * @param  {[type]} function(    [description]
 * @return {[type]}              [description]
 */
gulp.task('minifyHtml', function(){
    // 压缩index文件
    gulp.src('./views/**/*.html')
        .pipe(minifyHtml({empty:true, quotes: true}))
        .pipe(gulp.dest('./build/views'));
});

/**
 * cache
 * @param  {[type]} 'default' [description]
 * @param  {[type]} function  (             [description]
 * @return {[type]}           [description]
 */
gulp.task('cache',  function () {
  var stream = gulp.src('./views/**/*.html')
    .pipe(templateCache('templates.js', {module: 'App', root: '../views/'}))
    .pipe(gulp.dest('./js/'));

  return stream;

});


/**
 * 拷贝图片目录
 */
gulp.task('copyImagesFolder', function(){
    // 图片排量拷贝
    var stream = gulp.src('./styles/images/**/*')
        .pipe(gulp.dest(paths.buildPath + '/styles/images/'));

    var stream = gulp.src('./styles/fonts/**/*')
        .pipe(gulp.dest(paths.buildPath + '/styles/fonts/'));    
});

// Static Server + watching scss/html files
gulp.task('serve', ['less'], function() {

    browserSync.init({
        server: "./"
    });

    gulp.watch("./styles/*.less", ['less']);
    gulp.watch("./*.html").on('change', browserSync.reload);
});


gulp.task('default', ['serve']);

// 打包文件
gulp.task('build', ['cache', 'copyImagesFolder', 'usemin']);
